package org.cscarioni

class EstimatesService {

    boolean transactional = true
    /**
     * Save Estimates for entity.
     * Should be invoked in a scheduled task at end of date... maybe also ever that a change is made in a task
     */
    def saveEstimatesForDayAndEntity(date,entity,project) {
    	entity.estimatedTimes.each{
    		if(it.day.format('dd/MM/yyyy')==date.format('dd/MM/yyyy')){
    			it.estimatedTime=entity.storyPointsLeft
				return;
			}
		}
		def estimatedTime=resolveEstimatedTimeEntityForObject(entity,date,project)
		estimatedTime.save()
		entity.addToEstimatedTimes(estimatedTime)
		entity.save()
	}
	
	def resolveEstimatedTimeEntityForObject(entity,day,project){
		if(entity instanceof Sprint){
			return new EstimatedTimeHistoryForSprint(day:day,estimatedTime:entity.storyPointsLeft,entity:entity,project:project)
		}
		else if(entity instanceof User){
			return new EstimatedTimeHistoryForUser(day:day,estimatedTime:entity.storyPointsLeft,entity:entity,project:project)
		}
		else if(entity instanceof Team){
			return new EstimatedTimeHistoryForTeam(day:day,estimatedTime:entity.storyPointsLeft,entity:entity,project:project)
		}
	}
}
